package com.momo.mal.dashboard;

import java.util.List;

import net.momo.mal.R;
import com.momo.mal.anime.adapter.HistoryAdapter;
import com.momo.mal.database.MalDatabaseAdapter;
import com.momo.mal.entity.Anime;
import com.momo.mal.entity.History;


import org.json.JSONObject;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class AnimeFragment extends Fragment {
	private static final String KEY_CONTENT = "AnimeFragment:Content";
	private MalDatabaseAdapter dbAdapter;
	private ListView listView;
	private TextView notif;
	
    public static AnimeFragment newInstance() {
    	AnimeFragment fragment = new AnimeFragment();
    	
        return fragment;
    }

    private String mContent = "???";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        dbAdapter = new MalDatabaseAdapter(getActivity());

        if ((savedInstanceState != null) && savedInstanceState.containsKey(KEY_CONTENT)) {
            mContent = savedInstanceState.getString(KEY_CONTENT);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dashboard_history, null);
        
        List<History> histories = dbAdapter.getHistory("anime");
        HistoryAdapter adapter = new HistoryAdapter(histories, getActivity());
        
        notif = (TextView) view.findViewById(R.id.text_notif);
        listView = (ListView) view.findViewById(R.id.list_history);
		listView.setAdapter(adapter);
		
		if(histories.size() == 0) {
			listView.setVisibility(ListView.GONE);
			
			notif.setVisibility(TextView.VISIBLE);
		}
        
        return view;
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putString(KEY_CONTENT, mContent);
    }
    
}
